<?php
/**
 * Create by Phpstorm
 * User: Peco
 * Date: 2019/12/17
 * Time: 20:57
 */

namespace App\Repositories;

use App\Models\AuctionGoodsInfo;
use App\Models\Goods;
use App\Models\GoodsCategory;
use App\Models\GoodsSku;
use App\Models\UserAmount;
use App\Models\UserIncreaseamountRecord;
use App\Models\UserWithDraw;

class PayRepository extends BaseRepositories
{
    /**
     * @var Goods 注入的Goods model
     */
    protected $goods;

    /**
     * @var GoodsCategory
     */
    protected $goodsCategory;

    /**
     * @var AuctionGoodsInfo
     */
    protected $auctionGoodsInfo;

    /**
     * @var GoodsSku
     */
    protected $goodsSku;

    /**
     * @var UserAmount 注入的 model
     */
    protected $userAmount;

    /**
     * @var UserIncreaseamountRecord 注入的 model
     */
    protected $userIncreaseamountRecord;

    /**
     * @var UserWithDraw 注入的 model
     */
    protected $userWithDraw;

    /**
     * GoodsRepository constructor.
     * @param Goods $goods
     */
    public function __construct(Goods $goods, GoodsCategory $goodsCategory,
                                AuctionGoodsInfo $auctionGoodsInfo,
                                GoodsSku $goodsSku, UserAmount $userAmount,
                                UserIncreaseamountRecord $userIncreaseamountRecord,
                                UserWithDraw $userWithDraw){
        $this->goods = $goods;
        $this->goodsCategory = $goodsCategory;
        $this->auctionGoodsInfo = $auctionGoodsInfo;
        $this->goodsSku = $goodsSku;
        $this->userAmount = $userAmount;
        $this->userIncreaseamountRecord = $userIncreaseamountRecord;
        $this->userWithDraw = $userWithDraw;
    }

    /**
     * 用户资金消费明细
     * @param $limit
     * @param array $data
     * @return mixed
     */
    public function userAmountPageRepository($limit, array $data){
        return $this->userAmount
            ->with(['user'])
            ->when($data['phone'], function ($q)use($data){
                $q->whereHas('user',function ($q)use($data){
                    $q->where('phone', $data['phone']);
                });
            })
            ->orderByDesc('updateTime')
            ->paginate($limit);
    }

    /**
     * 用户积分明细
     * @param $limit
     * @param array $data
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function userIntegralPageRepository($limit, array $data){
        return $this->userIncreaseamountRecord
            ->with(['user'])
            ->when($data['phone'], function ($q)use($data){
                $q->whereHas('user',function ($q)use($data){
                    $q->where('phone', $data['phone']);
                });
            })
            ->orderByDesc('updateTime')
            ->paginate($limit);
    }

    /**
     * 用户提现明细列表
     * @param $limit
     * @param array $data
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function userWithdrawPageRepository($limit, array $data){
        return $this->userWithDraw
            ->with(['user'])
            ->when($data, function ($q)use($data){
                $q->where($data);
            })
            ->orderByDesc('updateTime')
            ->paginate($limit);
    }

    /**
     * 获取
     * @return mixed
     */
    public function getTradingCountRepository(){
        return $this->userAmount->count('amount');
    }

}
